<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>我的听课本</title>
    <script src="jquery.min.js"> </script>
    <style type="text/css">
    </style>
</head>
<body>

<div class="load-div">
    <h2></h2>
</div>
</body>
</html>
<script>
    window.onpageshow =function(event){
        if(event.persisted){
            window.location.reload()
        }
    }
    setTimeout('getTeacherAndGoHomePage()',200);

    function getQueryVariable(variable){
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i=0;i<vars.length;i++) {
            var pair = vars[i].split("=");
            if(pair[0] == variable){return pair[1];}
        }
        return(false);
    }

    function setCookie(name,value)
    {
        var Days = 30;
        var exp = new Date();
        exp.setTime(exp.getTime() + Days*24*60*60*1000);
        document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
    }
    function getCookie(name)
        {
            var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");

            if(arr=document.cookie.match(reg))

                return unescape(arr[2]);
            else
                return null;
        }

    //getTeacher
    function getTeacherAndGoHomePage(){
        //去后端获取weixinUserId，并初始化数据
        var code=getQueryVariable("code");
        console.log('code===',code)
        setCookie("code",code);
        getLoginUserInfoByWeixinCodeAndGoHomePage(code);
    }
    function getLoginUserInfoByWeixinCodeAndGoHomePage(code){
        $.ajax({
                   url: "user/getLoginUserInfoByWeixinCode",
                   type: "get",
                   data: {
                       code: code
                   },
                   dataType: "json",
                   success: function (responseJsonStr) {
                       if(!commomCheckLogin(responseJsonStr)){
                           return false;
                       }
                       if(responseJsonStr.data){
                           selectOuId(responseJsonStr.data);//选择学校
                       }

                   },
                   error: function (error) {
                       console.log("登录失败");
                       document.write(" 登录失败");
                   }
               });
    }
    //应用后台通用结果处理
    function commomCheckLogin(responseJsonStr){
       console.log('responseJsonStr==',responseJsonStr)
        if(responseJsonStr.status==1403){
            document.write(responseJsonStr.message);
            return false;
        } else if(responseJsonStr.status==1401){
            var h = window.location.href.split("/afterLoginWeixin.html")[0];
            window.location.href = h+"/beforeLoginWeixin.html";
            return false;
        }
        else{
            return true;
        }
    }
    //看有不有兼职，有就选，没有就默认
    function selectOuId(responseJsonStr) {
        //将weixinUserId设置在cookie里,如果不存在，则提示，不进行后续操作
        //将teacherId设置在cookie里,如果不存在，则提示，不进行后续操作
        //确定schoolId和ouId,并设置在cookie里，如果不确定，需要在界面选择
        setCookie("qyWeixinUserIdToken",responseJsonStr.wxUserId);
        setCookie("teacherIdToken",responseJsonStr.teacherId);
        setCookie("wxToken", responseJsonStr.wxToken);
        console.log("responseJsonStr===",responseJsonStr);
        if(responseJsonStr.orgUserVoList!=null
           &&responseJsonStr.orgUserVoList.length!=0){
            //弹窗选择-----模拟
            //选择完成
            if(responseJsonStr.orgUserVoList.length==1){
                setCookie("ouIdToken",responseJsonStr.orgUserVoList[0].ouId);
                setCookie("schoolIdToken",responseJsonStr.orgUserVoList[0].schoolId);
                setCookie("schoolZoneName",responseJsonStr.orgUserVoList[0].schoolZoneName);
                let codeEvaluateId = getCookie('codeEvaluateId');
                let teacherId = getCookie("teacherIdToken");
//                goIntoPageAfterAuthAll()
                if(codeEvaluateId){
                    var h = window.location.href.split("/afterLoginWeixin.html")[0];
                    window.location.href = `${h}/index.html#/myLesson/add/?wx=${teacherId}`;
//                    this.props.history.push(`/myLesson/add/?wx=${teacherId}`)
                } else {
                    goIntoPageAfterAuthAll()
                }

            }
             else {
                goIntoPageAfterAuthAll('goSchool')
            }
        }else{
            setCookie("schoolIdToken",responseJsonStr.schoolId);
            setCookie("ouIdToken",responseJsonStr.ouId);
            goIntoPageAfterAuthAll();//微信和应用端完全授权后进入首页
        }

    }

    //微信和应用端完全授权后进入首页
    function goIntoPageAfterAuthAll(type) {
        var h = window.location.href.split("/afterLoginWeixin.html")[0];
        if (type) {
            window.location.href = h + "/index.html#/selectSchool";
        } else {
            window.location.href = h + "/index.html";
        }
    }

</script>
